package com.ruoyi.livesass;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.volcengine.model.beans.livesaas.Coupon;
import com.volcengine.model.beans.livesaas.CreateActivityCouponModel;
import com.volcengine.model.beans.livesaas.CreateCouponModel;
import com.volcengine.model.beans.livesaas.MenuMsgNew;
import com.volcengine.model.beans.livesaas.ProductAPIs;
import com.volcengine.model.beans.livesaas.ProductMsgAPI;
import com.volcengine.model.livesaas.request.CommonRequest;
import com.volcengine.model.livesaas.request.CreateActivityCouponsRequest;
import com.volcengine.model.livesaas.request.CreateCouponsRequest;
import com.volcengine.model.livesaas.request.DeleteActivityCouponsRequest;
import com.volcengine.model.livesaas.request.DeleteCouponsRequest;
import com.volcengine.model.livesaas.request.ExplainProductRequest;
import com.volcengine.model.livesaas.request.GetActivityAllCouponsPickDataRequest;
import com.volcengine.model.livesaas.request.GetActivityCouponPickDataRequest;
import com.volcengine.model.livesaas.request.ListCouponsRequest;
import com.volcengine.model.livesaas.request.ListProductCardsRequest;
import com.volcengine.model.livesaas.request.ModifyActivityMenusRequest;
import com.volcengine.model.livesaas.request.SendActivityCouponRequest;
import com.volcengine.model.livesaas.request.SendProductOrderMessageRequest;
import com.volcengine.model.livesaas.request.StopActivityCouponRequest;
import com.volcengine.model.livesaas.request.UpdateActivityCouponRequest;
import com.volcengine.model.livesaas.request.UpdateActivityProductV2Request;
import com.volcengine.model.livesaas.request.UpdateCouponRequest;
import com.volcengine.model.livesaas.response.CommonStatusResponse;
import com.volcengine.model.livesaas.response.CreateActivityCouponsResponse;
import com.volcengine.model.livesaas.response.CreateActivityCouponsResponse.CreateActivityCouponsResponseBody;
import com.volcengine.model.livesaas.response.CreateCouponsResponse;
import com.volcengine.model.livesaas.response.CreateCouponsResponse.CreateCouponsResponseBody;
import com.volcengine.model.livesaas.response.DeleteActivityCouponsResponse;
import com.volcengine.model.livesaas.response.DeleteActivityCouponsResponse.DeleteActivityCouponsResponseBody;
import com.volcengine.model.livesaas.response.DeleteCouponsResponse;
import com.volcengine.model.livesaas.response.DeleteCouponsResponse.DeleteCouponsResponseBody;
import com.volcengine.model.livesaas.response.GetActivityAllCouponsPickDataResponse;
import com.volcengine.model.livesaas.response.GetActivityAllCouponsPickDataResponse.GetActivityAllCouponsPickDataResponseBody;
import com.volcengine.model.livesaas.response.GetActivityCouponPickDataResponse;
import com.volcengine.model.livesaas.response.GetActivityCouponPickDataResponse.GetActivityCouponPickDataResponseBody;
import com.volcengine.model.livesaas.response.GetActivityProductAPIResponse;
import com.volcengine.model.livesaas.response.ListCouponsResponse;
import com.volcengine.model.livesaas.response.ListProductCardsResponse;
import com.volcengine.model.livesaas.response.ModifyActivityMenusResponse;
import com.volcengine.model.livesaas.response.SendActivityCouponResponse;
import com.volcengine.model.livesaas.response.SendActivityCouponResponse.SendActivityCouponResponseBody;
import com.volcengine.model.livesaas.response.SendProductOrderMessageResponse;
import com.volcengine.model.livesaas.response.StopActivityCouponResponse;
import com.volcengine.model.livesaas.response.StopActivityCouponResponse.StopActivityCouponResponseBody;
import com.volcengine.model.livesaas.response.UpdateActivityCouponResponse;
import com.volcengine.model.livesaas.response.UpdateActivityCouponResponse.UpdateActivityCouponResponseBody;
import com.volcengine.model.livesaas.response.UpdateCouponResponse;
import com.volcengine.model.livesaas.response.UpdateCouponResponse.UpdateCouponResponseBody;
import com.volcengine.service.livesaas.LivesaasService;
import com.volcengine.service.livesaas.impl.LivesaasServiceImpl;
/**
* @ClassName: ProductVideoService 
* @Description: TODO(商品相关接口) 
* @author 晏大兵 
* @date 2025年2月10日 上午11:13:06 
*/
@Component
@Order(2)
public class ProductVideoService implements ApplicationRunner {
	
	
	private static final Logger LOG = LoggerFactory.getLogger(ProductVideoService.class);

	LivesaasService livesaasService;
	
	//@Value("${custom.hsSubUserName}")
	//private String hsSubUserName = "dfhuf520@126.com";

	@Override
	public void run(ApplicationArguments args) throws Exception {
		livesaasService = LivesaasServiceImpl.getInstance();

		List<ProductMsgAPI> productList = Lists.newArrayList();
		
		//updateActivityProduct(1823665892762688L, productList);
		
		//this.activityTopProduct(1823665892762688L, 638365);
		
		//this.syncActivityCoupons();
		
		//sendActivityCoupons(1823665892762688L, "YWID0001", "测试券");
		
		//List<CreateCouponModel> couponList = Lists.newArrayList();
		
		//this.updateActivityProduct(1823665892762688L, productList);
		
	}
	


	
	
	/**
	 * 
	* @Title: SendProductOrderMessage 
	* @Description: TODO(向直播间发送商品购买消息) 
	* @param  zbjid 直播间ID
	* @param  spid 商品ID
	* @param  xdxm   下单姓名
	* @return void    返回类型 
	 * @throws Exception 
	* @throws
	 */
	public SendProductOrderMessageResponse sendProductOrderMessage(long zbjid, long spid, String xdxm) throws Exception {
		SendProductOrderMessageRequest sendProductOrderMessageRequest = new SendProductOrderMessageRequest();
		sendProductOrderMessageRequest.setActivityId(zbjid);
		sendProductOrderMessageRequest.setProductId(spid);
		sendProductOrderMessageRequest.setUserName(xdxm);
		SendProductOrderMessageResponse sendProductOrderMessageResponse = livesaasService.sendProductOrderMessage(sendProductOrderMessageRequest);
		return sendProductOrderMessageResponse;
	}
	

	
	/**
	 * 获取直播间内指定卡券领取信息
	 * @param Id 优惠券id
	 * @param ActivityId 直播间 ID。您可通过调用 ListActivityAPI 接口获取 ID。    
	 * @param PageNumber  1	分页查询页码。默认值为 1。取值大于等于 1。
	 * @param PageSize  10	分页查询数量。默认值为 10。取值范围为 [1,2000]。
	 * @return
	 */
	public GetActivityCouponPickDataResponseBody getGetActivityCouponPickData(Long yhqid,Long zbjid,Integer PageNumber,Integer PageSize) {
		GetActivityCouponPickDataResponseBody responseBody  = null;
		try {
			GetActivityCouponPickDataRequest request = new GetActivityCouponPickDataRequest();
			request.setActivityId(zbjid);
			request.setPageNumber(PageNumber);
			request.setPageSize(PageSize);
			request.setId(yhqid);
			GetActivityCouponPickDataResponse response = livesaasService.getActivityCouponPickData(request);
			responseBody =  response.getResult();
		} catch (Exception e) {
			LOG.error("获取直播间内指定卡券领取信息错误 Id["+yhqid+"], ActivityId["+zbjid+"], PageNumber["+PageNumber+"],  PageSize["+PageSize+"], errorMsg["+e.getMessage()+"]");
			e.printStackTrace();
		}
		return responseBody;
	}
	


	/**
	 * 获取直播间内所有卡券领取信息
	 * @param ActivityId 直播间 ID。您可通过调用 ListActivityAPI 接口获取 ID。    
	 * @param PageNumber  1	分页查询页码。默认值为 1。取值大于等于 1。
	 * @param PageSize  10	分页查询数量。默认值为 10。取值范围为 [1,2000]。
	 * @return
	 */
	public GetActivityAllCouponsPickDataResponseBody getActivityAllCouponsPickData(Long ActivityId,Integer PageNumber,Integer PageSize) {
		GetActivityAllCouponsPickDataResponseBody responseBody  = null;
		try {
			GetActivityAllCouponsPickDataRequest request = new GetActivityAllCouponsPickDataRequest();
			request.setActivityId(ActivityId);
			request.setPageNumber(PageNumber);
			request.setPageSize(PageSize);
			GetActivityAllCouponsPickDataResponse response = livesaasService.getActivityAllCouponsPickData(request);
			responseBody =  response.getResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return responseBody;
	}
	
	

	/**
	 * 下架福利券
	 * @param zbjid 直播间id
	 * @param zbjQuanId 券id
	 * @return
	 * @throws Exception
	 */
	public boolean stopActivityCoupon(long zbjid, long zbjQuanId) {
		StopActivityCouponRequest stopActivityCouponRequest = new StopActivityCouponRequest();
		stopActivityCouponRequest.setActivityId(zbjid);
		stopActivityCouponRequest.setId(zbjQuanId);
		StopActivityCouponResponse stopActivityCouponResponse = null;
		try {
			stopActivityCouponResponse = livesaasService.stopActivityCoupon(stopActivityCouponRequest);
		} catch (Exception e) {
			e.printStackTrace();
	        throw new RuntimeException("操作失败，请重试 " + e.getMessage());
		}
		StopActivityCouponResponseBody responseBody = stopActivityCouponResponse.getResult();
		if(responseBody==null) {
	        throw new RuntimeException("操作失败，请重试 responseBody == null");
		}
		if(responseBody.getStatus().booleanValue() == false) {
			com.volcengine.model.response.ResponseMetadata.Error error = 	stopActivityCouponResponse.getResponseMetadata().getError();
			if(error!=null) {
    	        throw new RuntimeException("操作失败，请重试code:"+error.getMessage()+" msg："+error.getMessage()+ "codeN" +error.getCodeN());
			}
	        throw new RuntimeException("操作失败，请重试 ResponseMetadata.Error == null");
		}
		return true;
	}
	

	
	

	
	
	/**
	 * 删除发送福利券
	 * @param zbjid
	 * @param id
	 * @return
	 */
	public boolean  deleteActivityCouponsFlq(long zbjid, long id){
		List<Long> ids = new ArrayList<Long>();
		ids.add(id);
		DeleteActivityCouponsRequest deleteActivityCouponsRequest = new DeleteActivityCouponsRequest();
		deleteActivityCouponsRequest.setActivityId(zbjid);
		deleteActivityCouponsRequest.setIds(ids);
		DeleteActivityCouponsResponse deleteActivityCouponsResponse =  null;
		try {
			deleteActivityCouponsResponse = livesaasService.deleteActivityCoupons(deleteActivityCouponsRequest);
		} catch (Exception e) {
			e.printStackTrace();
	        throw new RuntimeException("操作失败，请重试 " + e.getMessage());
		}
		DeleteActivityCouponsResponseBody responseBody = deleteActivityCouponsResponse.getResult();
		if(responseBody.getStatus().booleanValue() == false) {
			com.volcengine.model.response.ResponseMetadata.Error error = 	deleteActivityCouponsResponse.getResponseMetadata().getError();
			if(error!=null) {
    	        throw new RuntimeException("操作失败，请重试code:"+error.getMessage()+" msg："+error.getMessage()+ "codeN" +error.getCodeN());
			}
	        throw new RuntimeException("操作失败，请重试 ResponseMetadata.Error == null");
		}
		return true;
		
	}
	
	
	/**
	 *  跟新福利券
	 * @param zbjid 直播间
	 * @param id id
	 * @param couponId  券类型
	 * @param count 数量 
	 * @param qlx 券类型 1福利券 2时长券
	 * @param gksc 观看时长
	 * @return
	 */
	 
	public boolean  updateActivityCouponFlq(long zbjid, long id ,long couponId,long count,int qlx,int gksc){
		UpdateActivityCouponRequest updateActivityCouponRequest = new UpdateActivityCouponRequest();
		updateActivityCouponRequest.setActivityId(zbjid);
		updateActivityCouponRequest.setAllowCloseIcon(false);
		updateActivityCouponRequest.setCount(count);
		if(qlx==1) {
			updateActivityCouponRequest.setRule(1);
		}
		if(qlx==2) {
			updateActivityCouponRequest.setRule(2);
			updateActivityCouponRequest.setDuration(gksc);
		}
		updateActivityCouponRequest.setId(id);
		updateActivityCouponRequest.setCouponId(couponId);
		UpdateActivityCouponResponse updateActivityCouponResponse =  null;
		try {
			updateActivityCouponResponse = livesaasService.updateActivityCoupon(updateActivityCouponRequest);
		} catch (Exception e) {
			e.printStackTrace();
	        throw new RuntimeException("操作失败，请重试 " + e.getMessage());
		}
		UpdateActivityCouponResponseBody responseBody = updateActivityCouponResponse.getResult();
		if(responseBody.getStatus().booleanValue() == false) {
			com.volcengine.model.response.ResponseMetadata.Error error = 	updateActivityCouponResponse.getResponseMetadata().getError();
			if(error!=null) {
    	        throw new RuntimeException("操作失败，请重试code:"+error.getMessage()+" msg："+error.getMessage()+ "codeN" +error.getCodeN());
			}
	        throw new RuntimeException("操作失败，请重试 ResponseMetadata.Error == null");
		}
		return true;
		
	}
	
	/**
	 * 创建福利券
	 * @param zbjid 直播间
	 * @param couponId  券类型
	 * @param count 数量 
	 * @param qlx 券类型 1福利券 2时长券
	 * @param gksc 观看时长
	 * @return
	 */
	public Long  createActivityCouponsFlq(long zbjid, long couponId,long count,int qlx,int gksc){
		CreateActivityCouponsRequest createActivityCouponsRequest = new CreateActivityCouponsRequest();
		createActivityCouponsRequest.setActivityId(zbjid);
		List<CreateActivityCouponModel> activityCoupons = Lists.newArrayList();
		CreateActivityCouponModel createActivityCouponModel = new CreateActivityCouponModel();
		createActivityCouponModel.setAllowCloseIcon(false);
		createActivityCouponModel.setCount(count);
		if(qlx==1) {
			createActivityCouponModel.setRule(1);
		}
		if(qlx==2) {
			createActivityCouponModel.setRule(2);
			createActivityCouponModel.setDuration(gksc);
		}
		createActivityCouponModel.setCouponId(couponId);
		activityCoupons.add(createActivityCouponModel);
		createActivityCouponsRequest.setActivityCoupons(activityCoupons);
		 
		 CreateActivityCouponsResponse createActivityCouponsResponse =  null;
		try {
			createActivityCouponsResponse = livesaasService.createActivityCoupons(createActivityCouponsRequest);
		} catch (Exception e) {
			e.printStackTrace();
	        throw new RuntimeException("操作失败，请重试 " + e.getMessage());
		}
		CreateActivityCouponsResponseBody responseBody = createActivityCouponsResponse.getResult();
		if(responseBody==null || responseBody.getIds()==null) {
	        throw new RuntimeException("操作失败，请重试 responseBody == null");
		}
		List<Long> ids = responseBody.getIds();
		if(responseBody.getStatus().booleanValue() == false || ids.size()==0) {
			com.volcengine.model.response.ResponseMetadata.Error error = 	createActivityCouponsResponse.getResponseMetadata().getError();
			if(error!=null) {
    	        throw new RuntimeException("操作失败，请重试code:"+error.getMessage()+" msg："+error.getMessage()+ "codeN" +error.getCodeN());
			}
	        throw new RuntimeException("操作失败，请重试 ResponseMetadata.Error == null");
		}
		return ids.get(0);
		
	}
	
	

	/**
	 * 上架福利券
	 * @param zbjid 直播间id
	 * @param zbjQuanId 券id
	 * @return
	 * @throws Exception
	 */
	public boolean sendActivityCoupon(long zbjid, long zbjQuanId){
		SendActivityCouponRequest sendActivityCouponRequest = new SendActivityCouponRequest();
		sendActivityCouponRequest.setActivityId(zbjid);
		sendActivityCouponRequest.setId(zbjQuanId);
		
		
		SendActivityCouponResponse sendActivityCouponResponse = null;
		try {
			sendActivityCouponResponse = livesaasService.sendActivityCoupon(sendActivityCouponRequest);
		} catch (Exception e) {
			e.printStackTrace();
	        throw new RuntimeException("操作失败，请重试 " + e.getMessage());
		}
		LOG.debug("发放券码 | " + JSONObject.toJSONString(sendActivityCouponResponse));
		SendActivityCouponResponseBody responseBody = sendActivityCouponResponse.getResult();
		if(responseBody==null) {
	        throw new RuntimeException("操作失败，请重试 responseBody == null");
		}
		if(responseBody.getStatus().booleanValue() == false) {
			com.volcengine.model.response.ResponseMetadata.Error error = 	sendActivityCouponResponse.getResponseMetadata().getError();
			if(error!=null) {
    	        throw new RuntimeException("操作失败，请重试code:"+error.getMessage()+" msg："+error.getMessage()+ "codeN" +error.getCodeN());
			}
	        throw new RuntimeException("操作失败，请重试 ResponseMetadata.Error == null");
		}
		return true;
	}
	
	
	
	/**
	 * 删除一个福利券
	 * 
	 * @return
	 * @throws Exception
	 */
	public boolean deleteCoupon(Long id)   {
		List<Long> ids = new ArrayList<Long>();
		ids.add(id);
		DeleteCouponsRequest deleteCouponsRequest = new DeleteCouponsRequest();
		deleteCouponsRequest.setIds(ids);
		DeleteCouponsResponse deleteCouponsResponse = null;
		try {
			deleteCouponsResponse = livesaasService.deleteCoupons(deleteCouponsRequest);
		} catch (Exception e) {
			e.printStackTrace();
	        throw new RuntimeException("操作失败，请重试 " + e.getMessage());
		}
				
		DeleteCouponsResponseBody responseBody = deleteCouponsResponse.getResult();
		if(responseBody==null) {
	        throw new RuntimeException("操作失败，请重试 responseBody == null");
		}
		if(responseBody.getStatus().booleanValue() == false) {
			com.volcengine.model.response.ResponseMetadata.Error error = 	deleteCouponsResponse.getResponseMetadata().getError();
			if(error!=null) {
    	        throw new RuntimeException("操作失败，请重试code:"+error.getMessage()+" msg："+error.getMessage()+ "codeN" +error.getCodeN());
			}
	        throw new RuntimeException("操作失败，请重试 ResponseMetadata.Error == null");
		}
		return true;
	}
	
	
	/**
	 * 
	 * 修改一个福利券
	 * @param name id
	 * @param name 福利券名称
	 * @param pic  福利券图片
	 * @param des  福利券描述
	 * @return
	 * @throws Exception
	 */
	public boolean updateCoupon(Long id,String name , String pic, String des) {
		UpdateCouponRequest updateCouponRequest = new UpdateCouponRequest();
		updateCouponRequest.setId(id);
		updateCouponRequest.setName(name);
		updateCouponRequest.setPic(pic);
		updateCouponRequest.setThirdPartyId("YWID0002");
		UpdateCouponResponse updateCouponResponse = null;
		try {
			updateCouponResponse = livesaasService.updateCoupon(updateCouponRequest);
		} catch (Exception e) {
			e.printStackTrace();
	        throw new RuntimeException("操作失败，请重试 " + e.getMessage());
		}
		UpdateCouponResponseBody responseBody = updateCouponResponse.getResult();
		if(responseBody==null) {
	        throw new RuntimeException("操作失败，请重试 responseBody == null");
		}
		if(responseBody.getStatus().booleanValue() == false) {
			com.volcengine.model.response.ResponseMetadata.Error error = updateCouponResponse.getResponseMetadata().getError();
			if(error!=null) {
    	        throw new RuntimeException("操作失败，请重试code:"+error.getMessage()+" msg："+error.getMessage()+ "codeN" +error.getCodeN());
			}
	        throw new RuntimeException("操作失败，请重试 ResponseMetadata.Error == null");
		}
		return true;
	}
	
	

	/**
	 * 
	 * 添加一个福利券
	 * @param name 福利券名称
	 * @param pic  福利券图片
	 * @param des  福利券描述
	 * @param subUserName  火山子账户ID
	 * @param ywid  业务ID
	 * @return
	 * @throws Exception
	 */
	public Long addActivityCoupon(String name , String pic, String des, String subUserName, String ywid) {
		List<CreateCouponModel> couponList = new ArrayList<CreateCouponModel>();
		//示例
		CreateCouponsRequest createCouponsRequest = new CreateCouponsRequest();
		CreateCouponModel createCouponModel = new CreateCouponModel();
		createCouponModel.setName(name);
		createCouponModel.setThirdPartyId(ywid);
		createCouponModel.setPic(pic);
		createCouponModel.setOwnerSubAccountName(subUserName);
		createCouponModel.setDescription(des);
		
		couponList.add(createCouponModel);
		
		createCouponsRequest.setCoupons(couponList);
		CreateCouponsResponse createCouponsResponse = null;
		try {
			createCouponsResponse = livesaasService.createCoupons(createCouponsRequest);
		} catch (Exception e) {
			e.printStackTrace();
	        throw new RuntimeException("操作失败，请重试 " + e.getMessage());
		}
		CreateCouponsResponseBody responseBody = createCouponsResponse.getResult();
		if(responseBody==null || responseBody.getIds()==null) {
	        throw new RuntimeException("操作失败，请重试 responseBody == null");
		}
		List<Long> ids = responseBody.getIds();
		if(responseBody.getStatus().booleanValue() == false || ids.size()==0) {
			com.volcengine.model.response.ResponseMetadata.Error error = 	createCouponsResponse.getResponseMetadata().getError();
			if(error!=null) {
    	        throw new RuntimeException("操作失败，请重试code:"+error.getMessage()+" msg："+error.getMessage()+ "codeN" +error.getCodeN());
			}
	        throw new RuntimeException("操作失败，请重试 ResponseMetadata.Error == null");
		}
		
		return ids.get(0);
	}
	
	
	
	/**
	 * 
	* @Title: addActivityCoupons 
	* @Description: TODO(将系统中的券同步至火山的券库中) 
	* @param  zbjid 直播间ID
	* @param  spid 商品ID
	* @param  xdxm   下单姓名
	* @return void    返回类型 
	 * @throws Exception 
	* @throws
	 
	public CreateCouponsResponse addActivityCoupons(List<CreateCouponModel> couponList, String subUserName, String ywid) throws Exception {
		
		//示例
		CreateCouponsRequest createCouponsRequest = new CreateCouponsRequest();
		CreateCouponModel createCouponModel = new CreateCouponModel();
		createCouponModel.setName("测试券2");
		createCouponModel.setThirdPartyId("YWID0002");
		createCouponModel.setPic("https://img2.baidu.com/it/u=769272126,1749845546&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500");
		createCouponModel.setOwnerSubAccountName(subUserName);
		createCouponModel.setDescription("测试券");
		
		couponList.add(createCouponModel);
		
		createCouponsRequest.setCoupons(couponList);
		CreateCouponsResponse createCouponsResponse = livesaasService.createCoupons(createCouponsRequest);
		
		
		LOG.debug("向卡券库添加券 | " + JSONObject.toJSONString(createCouponsResponse));
		
		return createCouponsResponse;
	}*/
	
	
	
	
	/**
	 * 
	* @Title: activityAddCoupon 
	* @Description: TODO(向直播间添加卡券) 
	* @param  zbjid 直播间ID
	* @param  quanId 券ID
	* @return void    返回类型 
	 * @throws Exception 
	* @throws
	 */
	public CreateActivityCouponsResponse activityAddCoupon(long zbjid, long quanId) throws Exception {
		
		//可根据券名称与券的业务ID查询出对应的券码的ID
		/*
		ListCouponsRequest listCouponsRequest = new ListCouponsRequest();
		listCouponsRequest.setName("手推车");
		listCouponsRequest.setPageNumber(1);
		listCouponsRequest.setPageSize(500);
		
		ListCouponsResponse listCouponsResponse = livesaasService.listCoupons(listCouponsRequest);
		LOG.debug("查询券码 | " + JSONObject.toJSONString(listCouponsResponse));
		
		List<Coupon> couponList = listCouponsResponse.getResult().getCoupons();
		long couponId = 0;
		
		for(Coupon coupon : couponList) {
			if(coupon.getThirdPartyId().equals("YWID001")) {
				couponId = coupon.getId();
				break;
			}
		}
		
		if(couponId == 0) {
			return null;
		}
		*/
		
		//向直播间添加卡券
		CreateActivityCouponsRequest createActivityCouponsRequest = new CreateActivityCouponsRequest();
		
		List<CreateActivityCouponModel> activityCoupons = Lists.newArrayList();
		
		CreateActivityCouponModel createActivityCouponModel = new CreateActivityCouponModel();
		createActivityCouponModel.setCount(-1l);
		createActivityCouponModel.setRule(2);
		createActivityCouponModel.setDuration(5);
		createActivityCouponModel.setCouponId(quanId);
		
		activityCoupons.add(createActivityCouponModel);
		
		createActivityCouponsRequest.setActivityId(zbjid);
		createActivityCouponsRequest.setActivityCoupons(activityCoupons);
		
		CreateActivityCouponsResponse createActivityCouponsResponse = livesaasService.createActivityCoupons(createActivityCouponsRequest);
		
		
		return createActivityCouponsResponse;
	}
	
	
	
	
	/**
	 * 
	* @Title: SendProductOrderMessage 
	* @Description: TODO(向直播间发送券) 
	* @param  zbjid 直播间ID
	* @param  quanId 券ID
	* @param  quanName 券名称
	* @return void    返回类型 
	 * @throws Exception 
	* @throws
	 */
	public CreateActivityCouponsResponse sendActivityCoupons(long zbjid, String quanId, String quanName) throws Exception {
		
		//先需要查询出对应的券码的ID
		ListCouponsRequest listCouponsRequest = new ListCouponsRequest();
		listCouponsRequest.setName(quanName);
		listCouponsRequest.setPageNumber(1);
		listCouponsRequest.setPageSize(500);
		
		ListCouponsResponse listCouponsResponse = livesaasService.listCoupons(listCouponsRequest);
		LOG.debug("查询券码 | " + JSONObject.toJSONString(listCouponsResponse));
		
		List<Coupon> couponList = listCouponsResponse.getResult().getCoupons();
		long couponId = 0;
		
		for(Coupon coupon : couponList) {
			if(coupon.getThirdPartyId().equals(quanId)) {
				couponId = coupon.getId();
				break;
			}
		}
		
		if(couponId == 0) {
			return null;
		}
		
		//向直播间创建卡券
		CreateActivityCouponsRequest createActivityCouponsRequest = new CreateActivityCouponsRequest();
		
		List<CreateActivityCouponModel> activityCoupons = Lists.newArrayList();
		
		CreateActivityCouponModel createActivityCouponModel = new CreateActivityCouponModel();
		createActivityCouponModel.setCount(-1l);
		createActivityCouponModel.setRule(2);
		createActivityCouponModel.setDuration(5);
		createActivityCouponModel.setCouponId(couponId);
		
		activityCoupons.add(createActivityCouponModel);
		
		createActivityCouponsRequest.setActivityId(zbjid);
		createActivityCouponsRequest.setActivityCoupons(activityCoupons);
		
		CreateActivityCouponsResponse createActivityCouponsResponse = livesaasService.createActivityCoupons(createActivityCouponsRequest);
		
		//向直播间发送卡券
		if(null != createActivityCouponsResponse && createActivityCouponsResponse.getResult().getIds().size() > 0) {
			SendActivityCouponRequest sendActivityCouponRequest = new SendActivityCouponRequest();
			sendActivityCouponRequest.setActivityId(zbjid);
			sendActivityCouponRequest.setId(createActivityCouponsResponse.getResult().getIds().get(0));
			
			SendActivityCouponResponse sendActivityCouponResponse = livesaasService.sendActivityCoupon(sendActivityCouponRequest);
			
			LOG.debug("发放券码 | " + JSONObject.toJSONString(sendActivityCouponResponse));
		}
		
		
		return createActivityCouponsResponse;
	}
	
	
	
	
	
	
	
	
	/**
	* @Title: updateActivityProduct 
	* @Description: TODO(更新直播间商品卡片-每次都全量提交 添加新品、上下架、置顶商品都采用此接口) 
	* @param   zbjid  直播间ID
	* @param   productList  商品列表
	* @return 返回类型 商品信息列表 JSON格式字符串
	 * @throws Exception 
	 setStrikethrough 商品原价
	 setHighlight 商品优惠价
	 setEnableStatus 1 上架  0 下架
	 setTitle 商品名称
	 setRemark 商品介绍
	 */
	public List<com.volcengine.model.livesaas.response.GetActivityProductAPIResponse.ProductMsgAPI> updateActivityProduct(long zbjid, List<ProductMsgAPI> productList) throws Exception {

		Map<String, ProductAPIs> productApiMap = Maps.newHashMap();
		ProductAPIs productAPIs  = new ProductAPIs();
		
		productAPIs.setEnableFloating(true);
		productAPIs.setProduct(productList);
		
		//目前12是固定写死
		productApiMap.put("12", productAPIs);
		
		
		UpdateActivityProductV2Request updateActivityProductV2Request = new UpdateActivityProductV2Request();
		updateActivityProductV2Request.setActivityId(zbjid);
		updateActivityProductV2Request.setProducts(productApiMap);
		
		
		CommonStatusResponse commonStatusResponse = livesaasService.updateActivityProductAPI(updateActivityProductV2Request);
		
		LOG.debug("添加商品 | " + JSONObject.toJSONString(commonStatusResponse));
		
		
		CommonRequest commonRequest = new CommonRequest();
		commonRequest.setActivityId(zbjid);
		
		GetActivityProductAPIResponse getActivityProductAPIResponse = livesaasService.getActivityProductAPI(commonRequest);
		
		//String resultProductJson = JSONObject.toJSONString(getActivityProductAPIResponse.getResult().get("12").getProduct());
		//LOG.debug("商品信息 | " + resultProductJson);
		
		
		return getActivityProductAPIResponse.getResult().get("12").getProduct();
		
		
	}
	
	/**
	* @Title: activityTopProduct 
	* @Description: TODO(向直播间置顶（讲解）商品卡片) 
	* @param  zbjid   直播间ID 
	* * @param  spid   商品ID
	* @return void    返回类型 
	 * @throws Exception 
	* @throws
	 */
	public void activityTopProduct(long zbjid, long spid,int floatingStatus) throws Exception {
		
		ExplainProductRequest explainProductRequest = new ExplainProductRequest();
		explainProductRequest.setActivityId(zbjid);
		explainProductRequest.setProductId(spid);
		explainProductRequest.setExplainStatus(1);
		explainProductRequest.setFloatingStatus(floatingStatus);
		
		LOG.debug("商品置顶 explainProductRequest| " + JSONObject.toJSONString(explainProductRequest));
		try {
			CommonStatusResponse commonStatusResponse = livesaasService.explainProductAPI(explainProductRequest);
			LOG.debug("商品置顶 commonStatusResponse | " + JSONObject.toJSONString(commonStatusResponse));
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		
	}
	

	/**
	* @Title: activityTopProduct 
	* @Description: TODO(向直播间开启菜单-目前默认只负责开启和关闭商品菜单) 
	* @param  zbjid   直播间ID 
	* @param  sfkq   是否开启
	* @return void    返回类型 
	 * @throws Exception 
	* @throws
	* 	1：图文介绍
		2：商品卡片
		3：聊天互动
		4：互动工具
		5：聚合直播
		6：互动问答
		7：内嵌链接
		8：私聊互动
	 */
	public ModifyActivityMenusResponse activityProductEnable(long zbjid, boolean sfkq) throws Exception {
		ModifyActivityMenusRequest modifyActivityMenusRequest = new ModifyActivityMenusRequest();
		modifyActivityMenusRequest.setActivityId(zbjid);
		
		List<MenuMsgNew> menuList = Lists.newArrayList();
		
		
		//商品管理比较特殊需要配合 ToolId
		if(sfkq) {
			
			ListProductCardsRequest listProductCardsRequest  = new ListProductCardsRequest();
			listProductCardsRequest.setActivityId(zbjid);
			ListProductCardsResponse listProductCardsResponse = livesaasService.getActivityProducts(listProductCardsRequest);
			
			LOG.debug("商品卡片信息 | " + JSONObject.toJSONString(listProductCardsResponse));
			
			if(listProductCardsResponse.getResult().getProductConfigs().size() > 0 ) {
				long toolid = listProductCardsResponse.getResult().getProductConfigs().get(0).getId();
				MenuMsgNew spMenu = new MenuMsgNew();
				spMenu.setName("商品列表");
				spMenu.setType(2);
				spMenu.setToolId(toolid);
				
				menuList.add(spMenu);
			}

		}
		
		MenuMsgNew kqMenu = new MenuMsgNew();
		kqMenu.setName("福利券");
		kqMenu.setType(4);
		
		MenuMsgNew ltMenu = new MenuMsgNew();
		ltMenu.setName("聊天弹幕");
		ltMenu.setType(3);
		
		menuList.add(kqMenu);
		menuList.add(ltMenu);
		
		modifyActivityMenusRequest.setMenus(menuList);
		
		ModifyActivityMenusResponse activityProductEnable =  livesaasService.modifyActivityMenus(modifyActivityMenusRequest);
		
		LOG.debug("菜单更新 | " + JSONObject.toJSONString(activityProductEnable));
		
		
		return activityProductEnable;
		
	}

}
